package test;

class Test{
    private static volatile Test t;
    private static final Object lock = new Object();
    private int num = 0;
    private Test(){}
    public static Test getTest(){
        if(t == null){
            synchronized (lock){
                if(t == null){
                    t = new Test();
                }
            }
        }
        return t;
    }

    public int getNum(){
        return this.num;
    }

    public void setNum(int num) {
        this.num = num;
    }
}

class Test1{
    private int num = 0;
    private Test1 t1;
    private final Object lock = new Object();

    public Test1 getT1() {
        if(t1 == null){
            synchronized (lock){
                if(t1 == null){
                    t1 = new Test1();
                }
            }
        }
        return t1;
    }

    public int getNum() {
        return num;
    }

    public void setNum(int num) {
        this.num = num;
    }
}

public class Demo2 {
    public static void main(String[] args) {
        Test t = Test.getTest();
        System.out.println(t.getNum());
        t.setNum(10);
        System.out.println(t.getNum());
        t = null;
        t = Test.getTest();
        System.out.println(t.getNum());

        System.out.println("======================");

        Test1 t1 = new Test1();
        System.out.println(t1.getNum());
        t1.setNum(11);
        System.out.println(t1.getNum());
        System.out.println(t1);
        t1 = null;
        t1 = new Test1();
        System.out.println(t1.getNum());
    }
}
